﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using QLTTTH.Entity;
using QLTTTH.Config;
using System.Data;
using System.Data.SqlClient;

namespace QLTTTH.DAO
{
    class NhanVienDAO:DAOBase
    {
        public DataSet GetAll()
        {
            return Select("selenv * from NhanVien");
        }

        public NhanVien GetById(String manv)
        {
            NhanVien nv = null;
            SqlConnection conn = null;
            try
            {
                conn = Config.Config.getConnection();
                conn.Open();
                SqlCommand commnad = new SqlCommand("selenv * from NhanVien where MaCT = '" + manv + "'", conn);
                SqlDataReader dataReader = commnad.ExecuteReader();
                if (dataReader.Read())
                {
                    nv = new NhanVien();
                    nv.MaNV = dataReader.GetString(0);
                    nv.TenNV = dataReader.GetString(1);
                    nv.GioiTinh = dataReader.GetBoolean(2);
                    nv.NgaySinh = dataReader.GetDateTime(3);
                    nv.DiaChi = dataReader.GetString(4);
                    nv.Sdt = dataReader.GetString(5);
                    nv.SoCmnd = dataReader.GetString(7);
                    nv.Luong = dataReader.GetFloat(8);
                    nv.ChucVu = dataReader.GetString(9);
                    nv.Status = dataReader.GetBoolean(10);
                    return nv;
                }
            }
            catch (Exception ex)
            {

            }
            finally
            {
                conn.Close();

            }
            return nv;
        }

        public bool Insert(NhanVien nv)
        {

            String sql = "INSERT INTO [QLVT].[dbo].[NhanVien]([MaNV],[TenNV],[GioiTinh],[NgaySinh],[DiaChi],[Sdt],[Socmnd],[Luong],[ChucVu],[Status])"
               + "VALUES('" + nv.MaNV + "','" + nv.TenNV + "'," + nv.GioiTinh + ",'" + nv.NgaySinh + "','" + nv.DiaChi + "'," + nv.Sdt + "'," + nv.SoCmnd + "'," + nv.Luong + "'," + nv.ChucVu + "'," + nv.Status + ")";
            return Update(sql);
        }

        public bool DeleteById(String manv)
        {
            return Update("delete from NhanVien where MaCT = '" + manv + "'");
        }

        public bool UpdateById(NhanVien nv)
        {
            String sql = "update NhanVien "
                 + " set TenNV = '" + nv.TenNV + "'"
                 + " set GioiTinh = '" + nv.GioiTinh + "'"
                 + " set NgaySinh = '" + nv.NgaySinh + "'"
                 + " set DiaChi = '" + nv.DiaChi + "'"
                 + " set Sdt = '" + nv.Sdt + "'"
                 + " set SoCmnd = '" + nv.SoCmnd + "'"
                 + " set Luong = " + nv.Luong + ""
                 + " set ChucVu = '" + nv.ChucVu + "'"
                 + " set Status = " + nv.Status + ""
                 + " where MaCT =  '" + nv.MaNV + "'";
            return Update(sql);
        }
    }
}
